package com.nageoffer.shortlink.admin.toolkit;

import java.util.Random;

public final class RandomGenerator {
    // 定义随机字符串的字符集
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    /**
     * 生成指定长度的随机字符串
     * @return 随机字符串
     */
    public static String generateRandom() {
        return generateRandom(6);
    }

    /**
     * 生成指定长度的随机字符串
     * @param length 随机字符串的长度
     * @return 随机字符串
     */
    public static String generateRandom(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("Length must be greater than 0");
        }

        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(randomIndex));
        }

        return sb.toString();
    }
}
